﻿import axios from 'axios'
import Cookies from 'js-cookie'

const getToken = () => {
    return Cookies.get('Admin-Token');
}

const Axios = axios.create({
    baseURL: process.env.VUE_APP_BASE_API, // api
    // baseURL: "http://10.212.190.77:8080/", // api
    // baseURL: "http://192.168.6.47:80/", // api
    timeout: 100000,
    responseType: "json",
    withCredentials: true, // 是否允许带cookie这些
    headers: {
        // "Content-Type": "application/json;charset=utf-8"
        Authorization: 'Bearer ' + getToken()
    }
});

//请求拦截  设置统一header
Axios.interceptors.request.use(config => {
    // console.log(config);

    return config
}, error => {
    return Promise.reject(error)
});

//响应拦截  401 过期处理
Axios.interceptors.response.use(response => {
    // console.log(response.data.msg);

    const { code } = response.data;  //响应登录状态码

    // console.log(code);

    // 如果响应请求未登录，则返回登录页面

    return response
}, error => {
    console.log(error.response);

    const { status } = error.response;  //响应错误状态
    const { data } = error.response;  //响应错误详情


    return Promise.reject(error)
});

export default Axios;